import 'package:flutter_web_server/ImcludeServer.dart';
import 'dart:async';


class ServerHelp {
  // 获取本机IP
  static Future<String?> getLocalIpAddress() async {
    try {
      final List<NetworkInterface> interfaces = await NetworkInterface.list();
      for (NetworkInterface interface in interfaces) {
        for (InternetAddress address in interface.addresses) {
          if (address.type == InternetAddressType.IPv4 && address.address.startsWith("192.168")) {
            return address.address;
          }
        }
      }
    } catch (e) {
      print('Failed to get local IP address: $e');
    }
    return null;
  }

  // 获取可用端口
  static Future<int?> getAvailablePort() async {
    try {
      // 创建一个临时的ServerSocket，绑定到任何可用的端口
      final server = await ServerSocket.bind(InternetAddress.anyIPv4, 0);
      // 获取绑定的端口号
      final port = server.port;
      // 关闭临时ServerSocket
      await server.close();
      return port;
    } catch (e) {
      print('Failed to get an available port: $e');
    }
    return null;
  }

}
